/*
  网速仪表盘
  Autho:devstone
  Email:kevinlq0912@163.com
  QQ:2313828706
*/
import QtQuick 2.0
import "../util/MathUtil.js" as MathUtil

Item {

    id: netSpeed;
    width: parent.width
    height: parent.height

    property var m_netSpeedValue: 20
    property var m_bigRadius: Math.min(parent.width,parent.height)*0.4
    property var m_minValue: 0
    property var m_maxValue: 100
    property var m_unitText: "Mbps"


    function update(value)
    {
        m_netSpeedValue = value;
        canvas.requestPaint();
    }

    //背景设置
    Rectangle
    {
        id: bck
        anchors.fill: parent
        color: "#495C73"
    }

    Canvas
    {
        id: canvas;
        width: parent.width
        height: parent.height

        onPaint:
        {
            var ctx = getContext("2d");

            var width = canvas.width
            var height = canvas.height
            ctx.clearRect(0,0,width,height);
            ctx.save();
            //将原点移到中心点,方便绘制
            ctx.translate(width/2,height/2);

            drawSpeed(ctx);

            ctx.restore();
        }
    }

    function drawSpeed(ctx)
    {
        drawBigCircle(ctx);
        //        ites(ctx,45,120,m_bigRadius,"#244461");
    }

    //绘制外面大圆
    function drawBigCircle(ctx)
    {
        var Angle = 280
        ctx.beginPath();
        ctx.save();
        ctx.rotate( MathUtil.getRadian(130));

        var radial = ctx.createRadialGradient(0,0,m_bigRadius*0.6,0,0,m_bigRadius);
        radial.addColorStop(0,"#244461");
        radial.addColorStop(0.5,"#265882");
        radial.addColorStop(1,"#2D628A");
        ctx.fillStyle = radial;


        ctx.fillStyle = "#00F2B4"
        ctx.lineCap = "round"

        var point1 = MathUtil.getCircleCoordinate(m_bigRadius*0.8,0,0,Angle);
        var point2 = MathUtil.getCircleCoordinate(m_bigRadius*0.8,0,0,0);

        ctx.arc(0,0,m_bigRadius,0, MathUtil.getRadian(Angle));
        ctx.lineTo(point1[0],point1[1]);
//        ctx.arc(0,0, m_bigRadius*0.8,0,MathUtil.getRadian(Angle));
//        ctx.stroke();
//        ctx.beginPath();
//        ctx.lineTo(m_bigRadius,0);
        ctx.stroke();
        ctx.beginPath();
        ctx.moveTo(m_bigRadius,0);
        ctx.lineTo(point2[0],point2[1]);
        ctx.arc(0,0, m_bigRadius*0.8,0,MathUtil.getRadian(Angle));
//        ctx.closePath();
//                ctx.fill();
        ctx.stroke();
        ctx.restore();
    }

    //圆
    function ites(ctx,a,b,r,color){
        ctx.beginPath();
        ctx.moveTo(0,0);
        ctx.arc(0,0,r,MathUtil.getRadian(a),MathUtil.getRadian(b),false);
        ctx.closePath();
        ctx.fillStyle = color;
        ctx.fill();
    }
}
